#include "SGMLoader.h"

namespace sgm
{
	Geometry * SGMLoader::LoadFromFile(const char *filename)
	{
		Geometry *geo = new Geometry();
		FileHolder *file = new FileHolder();
		file ->openToRead(filename);

		geo ->meshesCount = file ->readInt();
		geo ->meshes = new Mesh * [geo ->meshesCount];

		for (int i = 0; i < geo ->meshesCount; i++)
			geo ->meshes[i] = LoadMesh(file);

		file ->close();

		return geo;
	}

	Mesh * SGMLoader::LoadMesh(FileHolder *file)
	{
		Mesh *mesh = new Mesh();

		mesh ->id = file ->readInt();
		mesh ->materialIndex = file ->readInt();

		mesh ->verticesCount = file ->readInt();
		mesh ->vertices = new Verticle[mesh ->verticesCount];
		file ->readBuffer((BYTE*)mesh ->vertices, mesh ->verticesCount * sizeof(Verticle));

		return mesh;
	}
}
